package com.example.wecompose.ui.theme

import android.os.Build
import androidx.compose.animation.animateColorAsState
import androidx.compose.animation.core.animateFloatAsState
import androidx.compose.animation.core.tween
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.darkColorScheme
import androidx.compose.material3.dynamicDarkColorScheme
import androidx.compose.material3.dynamicLightColorScheme
import androidx.compose.material3.lightColorScheme
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.Immutable
import androidx.compose.runtime.staticCompositionLocalOf
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext

/*
private val DarkColorScheme = darkColorScheme(
    primary = Purple80,
    secondary = PurpleGrey80,
    tertiary = Pink80
)

private val LightColorScheme = lightColorScheme(
    primary = Purple40,
    secondary = PurpleGrey40,
    tertiary = Pink40

    /* Other default colors to override
    background = Color(0xFFFFFBFE),
    surface = Color(0xFFFFFBFE),
    onPrimary = Color.White,
    onSecondary = Color.White,
    onTertiary = Color.White,
    onBackground = Color(0xFF1C1B1F),
    onSurface = Color(0xFF1C1B1F),
    */
)

@Composable
fun WeComposeTheme(
    darkTheme: Boolean = isSystemInDarkTheme(),
    // Dynamic color is available on Android 12+
    dynamicColor: Boolean = true,
    content: @Composable () -> Unit
) {
    val colorScheme = when {
        dynamicColor && Build.VERSION.SDK_INT >= Build.VERSION_CODES.S -> {
            val context = LocalContext.current
            if (darkTheme) dynamicDarkColorScheme(context) else dynamicLightColorScheme(context)
        }

        darkTheme -> DarkColorScheme
        else -> LightColorScheme
    }

    MaterialTheme(
        colorScheme = colorScheme,
        typography = Typography,
        content = content
    )
}
*/

private val LightColorPalette = WecomposeColors(
    bottomBar = Gray50,
    background = Gray100,
    listItem = White,
    divider = Gray300,
    chatPage = Gray200,
    textPrimary = Gray800,
    textPrimaryMe = Gray900,
    textSecondary = Gray600,
    onBackground = Gray700,
    icon = Gray700,
    iconCurrent = Green500,
    badge = Red400,
    onBadge = White,
    bubbleMe = Green200,
    bubbleOthers = White,
    textFieldBackground = White,
    more = Gray500,
    chatPageBgAlpha = 0.1f
)

private val DarkColorPalette = WecomposeColors(
    bottomBar = Gray700,
    background = Gray900,
    listItem = Gray700,
    divider = Gray600,
    chatPage = Gray800,
    textPrimary = Gray200,
    textPrimaryMe = Gray100,
    textSecondary = Gray400,
    onBackground = Gray300,
    icon = Gray300,
    iconCurrent = Green300,
    badge = Red300,
    onBadge = White,
    bubbleMe = Gray700,
    bubbleOthers = Gray700,
    textFieldBackground = Gray800,
    more = Gray400,
    chatPageBgAlpha = 0.3f
)

private val NewYearColorPalette = WecomposeColors(
    bottomBar = NewYearRed,
    background = NewYearLightRed,
    listItem = Color(0xFFFFF8E1),
    divider = NewYearGold,
    chatPage = NewYearLightRed,
    textPrimary = NewYearDarkRed,
    textPrimaryMe = Color(0xFF5D4037),
    textSecondary = NewYearRed,
    onBackground = NewYearDarkRed,
    icon = Gray300,
    iconCurrent = NewYearGold,
    badge = NewYearGold,
    onBadge = NewYearDarkRed,
    bubbleMe = NewYearGold.copy(alpha = 0.3f),
    bubbleOthers = Color(0xFFFFF8E1),
    textFieldBackground = Color(0xFFFFF8E1),
    more = NewYearDarkRed,
    chatPageBgAlpha = 0.2f
)

@Immutable
class WecomposeColors(
    val bottomBar: Color,
    val background: Color,
    val listItem: Color,
    val divider: Color,
    val chatPage: Color,
    val textPrimary: Color,
    val textPrimaryMe: Color,
    val textSecondary: Color,
    val onBackground: Color,
    val icon: Color,
    val iconCurrent: Color,
    val badge: Color,
    val onBadge: Color,
    val bubbleMe: Color,
    val bubbleOthers: Color,
    val textFieldBackground: Color,
    val more: Color,
    val chatPageBgAlpha: Float,
)

private val LocalWeComposeColors = staticCompositionLocalOf <WecomposeColors>{
    LightColorPalette
}
object WeComposeTheme{
    val colors: WecomposeColors
    @Composable
    get() = LocalWeComposeColors.current

    enum class Theme{
        Light,Dark,NewYear
    }
}

@Composable
fun ProvideWeComposeColors(
    colors: WecomposeColors,
    content: @Composable () -> Unit,
){
    CompositionLocalProvider(LocalWeComposeColors provides colors,content = content)
}


@Composable
fun WeComposeTheme(
    theme: WeComposeTheme.Theme = if(isSystemInDarkTheme()) WeComposeTheme.Theme.Dark else WeComposeTheme.Theme.Light,
    content: @Composable ()-> Unit,
){
    val targetColors = when(theme){
        WeComposeTheme.Theme.Light -> LightColorPalette
        WeComposeTheme.Theme.Dark -> DarkColorPalette
        WeComposeTheme.Theme.NewYear -> NewYearColorPalette
    }



    val colors = WecomposeColors(
        bottomBar = animateColorAsState(targetColors.bottomBar, tween(600)).value,
        background = animateColorAsState(targetColors.background, tween(600)).value,
        listItem = animateColorAsState(targetColors.listItem, tween(600)).value,
        divider = animateColorAsState(targetColors.divider, tween(600)).value,
        chatPage = animateColorAsState(targetColors.chatPage, tween(600)).value,
        textPrimary = animateColorAsState(targetColors.textPrimary, tween(600)).value,
        textPrimaryMe = animateColorAsState(targetColors.textPrimaryMe, tween(600)).value,
        textSecondary = animateColorAsState(targetColors.textSecondary, tween(600)).value,
        onBackground = animateColorAsState(targetColors.onBackground, tween(600)).value,
        icon = animateColorAsState(targetColors.icon, tween(600)).value,
        iconCurrent = animateColorAsState(targetColors.iconCurrent, tween(600)).value,
        badge = animateColorAsState(targetColors.badge, tween(600)).value,
        onBadge = animateColorAsState(targetColors.onBadge, tween(600)).value,
        bubbleMe = animateColorAsState(targetColors.bubbleMe, tween(600)).value,
        bubbleOthers = animateColorAsState(targetColors.bubbleOthers, tween(600)).value,
        textFieldBackground = animateColorAsState(targetColors.textFieldBackground, tween(600)).value,
        more = animateColorAsState(targetColors.more, tween(600)).value,
        chatPageBgAlpha = animateFloatAsState(targetColors.chatPageBgAlpha, tween(600)).value,
    )

    ProvideWeComposeColors(colors){
        MaterialTheme(
            typography = Typography,
            content = content
        )
    }
}